#include "NetworkTools.h"

std::pair<cv::Mat, cv::Size> NetworkTool::ToolImagePreprocess(std::string imageSrc){
    cv::Mat oriImage = cv::imread(imageSrc);
    cv::Size oriSize = oriImage.size();

    /*
     self.transform = transforms.Compose([
          transforms.Resize((512, 512), Image.BICUBIC),
          transforms.ToTensor(),
          transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
      ])
    */
    cv::Mat tensorMat = cv::dnn::blobFromImage(oriImage, 1.0 / 255.0, cv::Size(512, 512),
                                               cv::Scalar(), true);
    tensorMat = (tensorMat - 0.5) / 0.5;
    return std::pair(tensorMat, oriSize);
}

void NetworkTool::toHWC(const std::vector<float> &buffer, std::vector<float> &outBuffer, cv::Size bufferSize){
    const size_t dotNumber = bufferSize.width * bufferSize.height;
    const float scale = 1 / 2.0 * 255.0;
    for(size_t i = 0, j = 0; i < dotNumber * 3; j++){
        // b, g, r
        outBuffer[i] = (buffer[j + 2 * dotNumber] + 1)* scale;
        outBuffer[i + 1] = (buffer[j + dotNumber] + 1)* scale;
        outBuffer[i + 2] = (buffer[j] + 1) * scale;
        i += 3;
    }
}
